import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * service 岗位维护与管理
 * @date: 2018-6-19


 */
import request from 'utils/request';
import { filterNullValueObject } from 'utils/utils';
import { CPAAS_PLATFORM } from 'utils/config';
/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_PLATFORM, "/v1");
/**
 * 获取部门信息
 * @async
 * @function gainInfo
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 部门id
 * @returns {object} fetch Promise
 */

export function gainInfo(_x) {
  return _gainInfo.apply(this, arguments);
}
/**
 * 查询部门的岗位信息
 * @async
 * @function search
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 部门id
 * @param {?string} param.postionCode - 岗位编码
 * @param {?string} param.postionName - 岗位名称
 * @returns {object} fetch Promise
 */

function _gainInfo() {
  _gainInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/").concat(params.unitId), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _gainInfo.apply(this, arguments);
}

export function search(_x2) {
  return _search.apply(this, arguments);
}
/**
 * 保存新增岗位信息
 * @async
 * @function saveAdd
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitCompanyId - 部门所属公司Id
 * @param {!number} params.unitId - 部门id
 * @param {!object[]} params.saveData - 新增岗位列表
 * @param {!string} param.saveData[].postionCode - 岗位编码
 * @param {!string} param.saveData[].postionName - 岗位名称
 * @param {!number} param.saveData[].orderSeq - 排序号
 * @param {?number} param.saveData[].parentPositionId - 父级岗位id
 * @param {?string} param.saveData[].parentPositionName - 父级岗位名称
 * @param {!number} [param.saveData[].supervisorFlag = 0] - 主岗标记
 * @param {!number} [param.saveData[].enabledFlag= 1] - 启用标记
 * @returns {object} fetch Promise
 */

function _search() {
  _search = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    var tenantId, unitId;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            tenantId = params.tenantId, unitId = params.unitId;
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/companies/units/").concat(unitId, "/positions"), {
              method: 'GET',
              query: filterNullValueObject(params)
            }));

          case 2:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _search.apply(this, arguments);
}

export function saveAdd(_x3) {
  return _saveAdd.apply(this, arguments);
}
/**
 * 更新岗位信息
 * @async
 * @function saveEdit
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 部门id
 * @param {!number} param.postionId - 岗位Id
 * @param {!object} params.data - 新增岗位列表
 * @param {!number} param.data.postionId - 岗位Id
 * @param {!string} param.data.postionCode - 岗位编码
 * @param {!string} param.data.postionName - 岗位名称
 * @param {!number} param.data.orderSeq - 排序号
 * @param {?number} param.data.parentPositionId - 父级岗位id
 * @param {?string} param.data.parentPositionName - 父级岗位名称
 * @param {!number} param.data.supervisorFlag - 主岗标记
 * @param {!number} param.data.enabledFlag - 启用标记
 * @returns {object} fetch Promise
 */

function _saveAdd() {
  _saveAdd = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var tenantId, unitCompanyId, unitId;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            tenantId = params.tenantId, unitCompanyId = params.unitCompanyId, unitId = params.unitId;
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/companies/").concat(unitCompanyId, "/units/").concat(unitId, "/positions"), {
              method: 'POST',
              body: _toConsumableArray(params.saveData)
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _saveAdd.apply(this, arguments);
}

export function saveEdit(_x4) {
  return _saveEdit.apply(this, arguments);
}
/**
 * 禁用岗位信息
 * @async
 * @function forbindLine
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitCompanyId - 部门所属公司Id
 * @param {!number} params.unitId - 部门id
 * @param {!number} param.positionId - 岗位Id
 * @param {!number} param.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _saveEdit() {
  _saveEdit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    var tenantId, unitCompanyId, unitId, positionId;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            tenantId = params.tenantId, unitCompanyId = params.unitCompanyId, unitId = params.unitId, positionId = params.positionId;
            return _context4.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/companies/").concat(unitCompanyId, "/units/").concat(unitId, "/positions/").concat(positionId), {
              method: 'PUT',
              body: _objectSpread({}, params.data)
            }));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _saveEdit.apply(this, arguments);
}

export function forbindLine(_x5) {
  return _forbindLine.apply(this, arguments);
}
/**
 * 启用岗位信息
 * @async
 * @function enabledLine
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitCompanyId - 部门所属公司Id
 * @param {!number} params.unitId - 部门id
 * @param {!number} param.positionId - 岗位Id
 * @param {!number} param.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _forbindLine() {
  _forbindLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    var tenantId, unitCompanyId, unitId, positionId;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            tenantId = params.tenantId, unitCompanyId = params.unitCompanyId, unitId = params.unitId, positionId = params.positionId;
            return _context5.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/companies/").concat(unitCompanyId, "/units/").concat(unitId, "/positions/disable/").concat(positionId), {
              method: 'POST',
              body: {
                positionId: params.positionId,
                objectVersionNumber: params.objectVersionNumber,
                _token: params._token
              }
            }));

          case 2:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _forbindLine.apply(this, arguments);
}

export function enabledLine(_x6) {
  return _enabledLine.apply(this, arguments);
}
/**
 * 处理接口获取的数据，提取每个节点的层次路径
 * @param {array} collections - 页面展示数据
 * @param {array} levelPath - 特定岗位的层级路径
 * @returns {object} 节点树和层次路径组成的对象
 */

function _enabledLine() {
  _enabledLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    var tenantId, unitCompanyId, unitId, positionId;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            tenantId = params.tenantId, unitCompanyId = params.unitCompanyId, unitId = params.unitId, positionId = params.positionId;
            return _context6.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/companies/").concat(unitCompanyId, "/units/").concat(unitId, "/positions/enable/").concat(positionId), {
              method: 'POST',
              body: {
                positionId: params.positionId,
                objectVersionNumber: params.objectVersionNumber,
                _token: params._token
              }
            }));

          case 2:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _enabledLine.apply(this, arguments);
}

export function renderTreeData(collections, levelPath) {
  var pathMap = levelPath || {};
  var renderTree = collections.map(function (item) {
    var temp = item;
    pathMap[temp.positionId] = [].concat(_toConsumableArray(pathMap[temp.parentPositionId] || []), [temp.positionId]);

    if (temp.children) {
      temp.children = _toConsumableArray(renderTreeData(temp.children, pathMap).renderTree);
    }

    return temp;
  });
  return {
    renderTree: renderTree,
    pathMap: pathMap
  };
}